<template>
  <div class="center">
    <div class="login-form">
      <div class="main-form">
        <el-form ref="form" :model="form">
          <div class="input-item">
            <el-input prefix-icon="iconfont icon-zhanghao" v-model="form.username" placeholder="用户名" clearable></el-input>
          </div>
          <div class="input-item">
            <el-input prefix-icon="iconfont icon-yincangmima" v-model="form.password" type="password" placeholder="密码" clearable></el-input>
          </div>
          <div class="input-item">
            <el-input v-model="form.captcha" placeholder="验证码" clearable></el-input>
          </div>
          <div class="input-item">
            <img :src="captchaBase64" alt="" @click="getCaptcha" />
            <el-input v-if="false" v-model="form.key"></el-input>
          </div>
          <div class="form-submit">
            <div class="form-submit-item">
              <el-button size="medium" :loading="loading" @click="handleLogin">登陆</el-button>
            </div>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import api from '/src/utils/request/BaseRequest'

  export default {
    name: "LoginComponent",
    components: {},
    data(){
      return {
        form: {
          username: 'admin',
          password: 'admin',
          key: '',
          captcha: ''
        },
        loading: false,
        rules:{
          username: [
            { required: true, trigger: "blur", message: "请输入您的账号" }
          ],
          password: [
            { required: true, trigger: "blur", message: "请输入您的密码" }
          ]
        },
        captchaBase64: '',
        captchaKey: ''
      }
    },
    created() {
    },
    mounted() {
      this.getCaptcha()
    },
    methods:{
      // 登录
      handleLogin(){
        const username = this.form.username;
        const password = this.form.password;
        if (username == null || username === ''){
          this.$message.warning("请输入帐号")
          return
        }
        if (password == null || password === ''){
          this.$message.warning("请输入登陆密码")
          return
        }
        this.loading = true;
        this.doLogin()
      },
      doLogin(){
        this.$store.dispatch('Login',this.form).then((res) => {
          this.loading = false;
        })
      },
      cancel(){
        this.form = {}
      },
      getCaptcha() {
        // api.get('/sys/auth/captcha').then(res => {
        //   this.captchaBase64 = res.data.image
        //   this.captchaKey = res.data.key
        //   this.form.key = res.data.key
        // }).catch(err => {
        //   console.log('err')
        // })
        this.$store.dispatch('getCaptcha',this.form).then((res) => {
          this.captchaBase64 = res.data.image
          this.captchaKey = res.data.key
          this.form.key = res.data.key
        })
      }
    }
  }
</script>

<style scoped>
  .center{
    width: 100%;
    height: 100%;
  }

  .login-form {
    position: relative;
    width: 100%;
    text-align: center;
    display: flex;
    justify-content: center;
    flex-direction: column;
  }

  .main-form{
    width: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
  }

  .input-item{
    display: flex;
    position: relative;
    height: auto;
    padding: 14px 0;
    justify-content: center;
    align-items: center;
  }

  ::v-deep .el-input input:hover {
    opacity: 1;
    display: inline-block;
    border: 0px;
  }
  ::v-deep .el-input__inner{
    height: 42px;
    display: inline-block;
    border: 0px;
    opacity: 1;
    background: var(--background-main);
  }
  ::v-deep .el-input input:focus {
    display: inline-block;
    border: 0px;
    opacity: 1;
  }
  ::v-deep .el-input--prefix .el-input__inner {
    padding-left: 50px;
  }
  ::v-deep .el-input__prefix{
    width: 50px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  ::v-deep .el-input__icon{
    font-size: 18px;
    color: var(--item-border-normal-color);
    display: block;
    line-height: 38px;
  }

  /* 表单忘记密码 */
  .form-link {
    width: auto;
    text-align: left;
  }
  .form-link span {
    font-size: 14px;
    color: #d0d0d0;
    position: relative;
    left: 25%;
  }
  .form-link span:hover {
    cursor: pointer;
    color: var(--item-border-hover-color);
    transition: 0.3s;
  }

  /* 表单 按钮 */
  .form-submit{
    width: 100%;
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    align-items: center;
    flex-direction: column;
    margin-top: 30px;
    margin-bottom: 30px;
  }
  .form-submit-item{
    width: 100%;
    margin: 8px 0;
  }
  ::v-deep .el-form{
    width: 65%;
    margin: 15px 0;
  }
  ::v-deep .el-button{
    width: 100%;
    height: 38px;
    font-size: 14px;
    letter-spacing: 8px;
    border-radius: 20px;
    color: var(--font-color-default);
  }
  ::v-deep .el-button--success{
    background-color: var(--item-border-active-color);
    border-color: var(--item-border-active-color);
  }
  ::v-deep .el-button--success:hover{
    background-color: var(--item-border-hover-color);
    border-color: var(--item-border-hover-color);
  }
  ::v-deep .el-button--info:hover{
    background-color: var(--item-border-default-color);
    border-color: var(--item-border-default-color);
  }
</style>
